package com.polaris.live.dialog

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.FreeGiftUserHeadAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogSendFreeGiftSelectBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.widget.seat.bean.SeatBean
import com.polaris.live.ui.party.widget.videoseat.bean.VideoSeatBean

/**
 * 赠送免费礼物选择弹窗
 *
 * @author Created by 半仙 on 2024/11/28/028 14:26
 */
class SendFreeGiftSelectDialog(
    context: Context,
    private val userBasicList: MutableList<UserBasicVo>,
    private val onSendGiftCallback: (UserBasicVo) -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId() = R.layout.dialog_send_free_gift_select

    private lateinit var mBinding: DialogSendFreeGiftSelectBinding

    private val mAdapter by lazy { FreeGiftUserHeadAdapter() }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogSendFreeGiftSelectBinding.bind(popupImplView)
        //过滤掉自己
        val indexof = userBasicList.indexOfFirst { it.userId == UserManager.getUserId() }
        if (indexof != -1) {
            userBasicList.removeAt(indexof)
        }
        mBinding.seatHeadRecycler.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = mAdapter.apply { setList(userBasicList) }
        }
        spanContent()
        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.setSelectPosition(position)
            spanContent()
        }
        mBinding.tvCancel.setOnSingleClickListener {
            dismiss()
        }
        mBinding.tvConfirm.setOnSingleClickListener {
            mAdapter.getSelectItem()?.let { it1 -> onSendGiftCallback.invoke(it1) }
            dismiss()
        }
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }
    }

    private fun spanContent() {
        val nickName = mAdapter.getSelectItem()?.nickname ?: ""
        val giftMessage = StringUtils.getString(string.sure_send_this_gift, nickName)  // 传入动态内容
        val spannableMessage = SpannableString(giftMessage)
        val startIndex = giftMessage.indexOf(nickName)
        val endIndex = startIndex + nickName.length  // 获取 "用户A" 的结束索引
        // 使用 ForegroundColorSpan 来设置字体颜色
        spannableMessage.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_ffffc300)),
            startIndex,  // 起始索引
            endIndex,  // 结束索引
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        mBinding.tvDetail.text = spannableMessage
    }

    companion object {

        fun show(
            context: Context,
            seatData: MutableList<SeatBean>? = null,
            videoSeatData: MutableList<VideoSeatBean>? = null,
            onSendGiftCallback: (UserBasicVo) -> Unit,
        ) {
            val userBasicList = when {
                seatData != null -> seatData.mapNotNull { it.userBasicVo }
                videoSeatData != null -> videoSeatData.mapNotNull { it.userBasicVo }
                else -> null
            }?.toMutableList()

            if (userBasicList.isNullOrEmpty()) return
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asCustom(SendFreeGiftSelectDialog(context, userBasicList, onSendGiftCallback))
                .show()
        }
    }

}